﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Manager.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="B2B.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">



</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HEAD" runat="server">
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="main_content" runat="server">
     <script type="text/javascript">

     

         function deleteOrder(madh, tr) {
             $.getJSON("/Manage/DeleteOrder?maDH=" + madh,
                 function (data) {
                     if (data == 1) {                        
                         $("#tblOrdersSent tr#tr" + tr).remove();                        
                     }
                 });
         }

         function loadOrdersSent(page){
           
             $.getJSON("/Manage/ordersSentJson?page=" + page,
                 function(data){
                     if(data.length >0){
                         var s ="";
                       //  s+="<table cellpadding='5px' border='0' >";
                         s+="<tr style='background-color:#177f75; font-size:17px;color:#a6fdbf'>";
                         s+= "<th style='width:50px'>Số đơn hàng</th>";
                         s+="<th style='width:50px'>Date </th>";
                         s+="<th style='width:50px'>Số Lượng đặt</th>";
                         s+="<th>FOB Price</th>";
                         s+="<th>Total amount</th>";
                         s+="<th>payment</th>";
                         s+="<th>Reciver</th>";
                         s+="<th>Tình Trạng</th>";
                         s+="<th></th>";
                         s+="<th></th>";
                         s+="</tr>";
                         var k =0;
                         for(i = 0; i <data.length; i++){
                             var chuoitinhtrang = "waiting";
                             if(data[i].TinhTrang ==1)
                                 chuoitinhtrang = "reject";
                             if(data[i].TinhTrang == 2)
                                 chuoitinhtrang = "Accept";
                             s+="<tr id='tr"+k+"' align='center'>";
                             s+="<td><input onclick='ChiTietDonHang("+ data[i].MaDonHang +")' type='button' value='" +data[i].MaDonHang+"' class='btn' /></td>";
                           //  s+="<td><input onclick='a()' type='button' value='" +data[i].MaDonHang+"' class='btn' /></td>";
                           
                           //  s+="<td>"+ data[i].MaDonHang +"</td>";
                             s+="<td>"+ data[i].NgayLap +"</td>";
                             s+="<td>" + data[i].SoLuongDat +"</td>";
                             s+="<td>"+data[i].Gia+"</td>";
                             s+="<td>"+data[i].TongTien+"</td>";
                             s+="<td>"+data[i].DaTraTruoc+"</td>";
                             s+="<td>"+data[i].EmailNguoiNhan+"</td>";
                             s+="<td>"+chuoitinhtrang +"</td>";

                             s+=" <td>";
                             if(data[i].TinhTrang == 2){
                                 s+="<input class='btn' type='button' value='Thanh Toán' />"
                                 s +="<a class='btn' href='/Manage/PaymentOrder/"+ data[i].MaDonHang +"'>Payment</a>"
                             }

                             if(data[i].TinhTrang==1){                        
                                 s+="<input class='btn' type='button' onclick='deleteOrder("+data[i].MaDonHang+","+k+")' value='Delete' />";                        
                             } 

                             s+="</td>";
                             s+="</tr>";
                             s+="<td colspan='9'>";
                             s+="   <div id='divChiTiet"+data[i].MaDonHang+"'></div>";
                             s+="   </td>";
                             s+="</tr>";
                             k++;
                         }
                         s+="<tr>";
                         s+=" <td colspan='10'>";
                         s+="</td>";
                         s+="</tr>";
                     }
                     $("#tblOrdersSent").html(s);
                 })
         }

         function loadOrdersRecived(page){
       
             $.getJSON("/Manage/ordersReciveJson?page=" + page,
                 function(data){
                   
                     if(data.length >0){
                         var s ="";
                     //    s+="<table cellpadding='5px' border='0' >";
                         s+="<tr style='background-color:#177f75; font-size:17px;color:#a6fdbf'>";
                         s+= "<th style='width:50px'>Số đơn hàng</th>";
                         s+="<th style='width:50px'>Date </th>";
                         s+="<th style='width:50px'>Số Lượng đặt</th>";
                         s+="<th>FOB Price</th>";
                         s+="<th>Total amount</th>";
                         s+="<th>Payment</th>";
                         s+="<th>Sender</th>";
                         s+="<th>Tình Trạng</th>";
                         s+="<th></th>";
                         s+="<th></th>";
                         s+="</tr>";
                
                         for(i = 0; i <data.length; i++){
                             var chuoitinhtrang = "waiting";
                             if(data[i].TinhTrang ==1)
                                 chuoitinhtrang = "reject";
                             if(data[i].TinhTrang == 2)
                                 chuoitinhtrang = "Accept";
                             s+="<tr align='center'>";
                             s+="<td><input onclick='ChiTietDonHang("+ data[i].MaDonHang +")' type='button' value='" +data[i].MaDonHang+"' class='btn' /></td>";

                        //     s+="<td><input onclick='a()' type='button' value='" +data[i].MaDonHang+"' class='btn' /></td>";

                          //   s+="<td>"+ data[i].MaDonHang +"</td>";
                             s+="<td>"+ data[i].NgayLap +"</td>";
                             s+="<td>" + data[i].SoLuongDat +"</td>";
                             s+="<td>"+data[i].Gia+"</td>";
                             s+="<td>"+data[i].TongTien+"</td>";
                             s+="<td>"+data[i].DaTraTruoc+"</td>";
                             s+="<td>"+data[i].EmailNguoiGui+"</td>";
                             s+="<td>"+chuoitinhtrang +"</td>";

                            
                            

                            s+="    <td>";
                            if(data[i].TinhTrang == 0){
                                s+="<form action='/Manage/UpdateOrder' method='post'>";
                                s+="<input class='btn' type='submit' value='Accept' />";
                                s+="<input type='hidden' value='"+data[i].MaDonHang+"' name='maDH' />";
                                s+="<input type='hidden' value='2' name='tinhTrang' />"; 
                                s+="</form>";
                            } 
                            s+="</td>";
                            s+="<td>";
                            if(data[i].TinhTrang==0){
                                s+="<form action='/Manage/UpdateOrder' method='post'>";
                                s+="<input class='btn' type='submit' value='Reject' />";
                                s+="<input type='hidden' value='"+data[i].MaDonHang+"' name='maDH' />";
                                s+="<input type='hidden' value='1' name='tinhTrang' />";
                                s+="</form>";
                            } 
                            s+="</td>";

                         
                            s+="</tr>";
                            s+="  <tr>";
                 s+="<td colspan='9'>";
                 s+="   <div id='divChiTiet"+data[i].MaDonHang+"'></div>";
                 s+="   </td>";
                 s+="</tr>";
                         }
                         s+="<tr>";
                         s+=" <td colspan='10'>";
                         s+="</td>";
                         s+="</tr>";
                     }
                     $("#tableorders").html(s);
                 })
         }

   
         function OrdersRecived() {
             $("#lnkOrdersRecived").click(function () {
                 $("#divOrdersRecived").css("display", "inline");
                 $("#divOrdersSent").css("display", "none");
             });
         }

         function OrdersSent() {
           
             $("#lnkOrdersSent").click(function () {
                 $("#divOrdersRecived").css("display", "none");
                 $("#divOrdersSent").css("display", "inline");
             });
         }

     

         function ChiTietDonHang(maDH){
             if($("#divChiTiet"+maDH).text().length==0){
                 $.getJSON("/Manage/orderDetail_Product?maDH=" + maDH,function(data){
                     
                     var s="";
                     s+="<center>";
                     s +="<table>";
                     s +="<tr>";
                     s +="<th> Product Name: </th>";
                     s +="<td>"+ data.TenSP +"</td>";                     
                     s +="</tr>";
                     s +="<tr>";
                     s +="<th>Product Image:</th>"
                     s +="<td><img src='/Images/Alibaba/"+ data.HinhAnh + "'/></td>";
                     s +="</tr>"
                     s +="<tr>";
                     s +="<th>File:</th>"
                     s +="<td><a href='/Files/Download?file="+data.BaoGia+"'>"+ data.BaoGia + "</a></td>";
                             

                     s +="</tr>";
                     s +="</table>";
                     s+="</center>";
                     $("#divChiTiet"+maDH).html(s);
                 });
             }
             $("#divChiTiet"+maDH).toggle();
           
            
         }
       
    </script>
    <%
        ArrayList ordersSent = (ArrayList)ViewData["list_Orders_Sent"];
        ArrayList ordersRecived = (ArrayList)ViewData["list_Orders_Recived"];
    %>
    <h6> Management -> Orders </h6>

     <a href="#" id="lnkOrdersRecived" onclick="OrdersRecived()"><font size="3"> Orders Recived </font></a> <b>|</b> 
    <a onclick="OrdersSent()" href="#" id="lnkOrdersSent"> <font size="3"> Orders Sent </font></a>
      <div id="a"></div>
    <div id="divOrdersSent" style="display:none">
        <table id="tblOrdersSent" cellpadding="5px" border="0" >
            <tr style='background-color:#177f75; font-size:17px;color:#a6fdbf'>
                <th style="width:50px">Số đơn hàng</th>
                <th style="width:50px">Date </th>
                <th style="width:50px">Số Lượng đặt</th>
                <th>FOB Price</th>
                <th>Total amount</th>
                <th>payment</th>
                <th>Reciver</th>
                <th>Tình Trạng</th>
                <th></th>
                <th></th>
            </tr>
            <%
                 int i = 0;
                
              foreach (var order in ordersSent)
              {
                  string maDonHang = ((SP_OrdersSent)order).MaDonHang.ToString();
                  string soLuongDat = ((SP_OrdersSent)order).SoLuongDat.ToString();
                  string gia = ((SP_OrdersSent)order).Gia.ToString();
                  string tongtien = ((SP_OrdersSent)order).TongTien.ToString();
                  string datra = ((SP_OrdersSent)order).DaTraTruoc.ToString();
                  string email = ((SP_OrdersSent)order).EmailNguoiNhan.ToString();
                  string ngayLap = ((SP_OrdersSent)order).NgayLap.ToString();
                  int tinhTrang = ((SP_OrdersSent)order).TinhTrang;
                  string chuoiTinhtrang = "";
                  switch (tinhTrang)
                  {
                      case 1: chuoiTinhtrang = "Reject"; break;
                      case 2: chuoiTinhtrang = "Accept"; break;
                      default: chuoiTinhtrang = "Waiting"; break;
                  }
            %>
                <tr id="tr<%=i %>" align="center">
                    <td>
                         <input onclick="ChiTietDonHang(<%=maDonHang %>)" type="button" value="<%=maDonHang %>" class="btn" />
   

                    </td>
                    <td><%=ngayLap %></td>
                    <td><%=soLuongDat %></td>
                    <td><%=gia %></td>
                    <td><%=tongtien %></td>
                    <td><%=datra %></td>
                    <td><%=email %></td>
                    <td><%=chuoiTinhtrang %></td>
                    

                          <td>
                        <%if(tinhTrang == 2){ %>
                     
                              <a class="btn" href="/Manage/PaymentOrder/<%=maDonHang %>">Payment</a>
                        <%} %>

                         <%if(tinhTrang==1){ %>                        
                        <input class="btn" type="button" onclick="deleteOrder(<%=maDonHang %>,<%=i %>)" value="Delete" />
                        <%;
                          } %>
                    </td>
                </tr>
                <tr>
                    <td colspan="10" >
                         <div id="divChiTiet<%=maDonHang %>"></div>
                    </td>
                </tr>
            <%} %>
            <tr>
                 <%
                    int tongtrangGui = int.Parse(ViewData["tongTrangGui"].ToString());
                    
                %>
                <td colspan="10">
                  

                </td>

            </tr>
        </table>
         
                    <div id="trigger-list">
                        <%for(int m = 0; m < tongtrangGui; m++)  {
                        %>
                            <input type="button"class="trigger error-trigger" value="<%=m+1 %>" onclick ="loadOrdersSent(<%=m%>)"/> 
                        <%} %>
                    </div>
    </div>

    <div id="divOrdersRecived">
        <table id="tableorders" border="0" cellpadding="5px">
            <tr style='background-color:#177f75; font-size:17px;color:#a6fdbf'>
                <th style="width:50px">Số đơn hàng</th>
                <th style="width:50px">Date </th>
                <th style="width:50px">Số Lượng đặt</th>
                <th>FOB Price</th>
                <th>Total amount</th>
                <th>Payment</th>
                <th>Sender</th>
                <th>Tình Trạng</th>
                <th></th>
            </tr>
              <%
                 int j = 0;
                 int k = 0;
              foreach (var order in ordersRecived)
              {
                  string maDonHang = ((SP_OrdersReciver)order).MaDonHang.ToString();
                  string soLuongDat = ((SP_OrdersReciver)order).SoLuongDat.ToString();
                  string gia = ((SP_OrdersReciver)order).Gia.ToString();
                  string tongtien = ((SP_OrdersReciver)order).TongTien.ToString();
                  string datra = ((SP_OrdersReciver)order).DaTraTruoc.ToString();
                  string email = ((SP_OrdersReciver)order).EmailNguoiGui.ToString();
                  string ngayLap = ((SP_OrdersReciver)order).NgayLap.ToString();
                  int tinhTrang = ((SP_OrdersReciver)order).TinhTrang;
                  string chuoiTinhtrang = "";
                  switch (tinhTrang)
                  {
                      case 1: chuoiTinhtrang = "Reject"; break;
                      case 2: chuoiTinhtrang = "Accept"; break;
                      default: chuoiTinhtrang = "Waiting"; break;
                  }
            %>
                <tr id="tr<%=k %>" align="center">
                    <td>
                       

                         <input onclick="ChiTietDonHang(<%=maDonHang %>)" type="button" value="<%=maDonHang %>" class="btn" />
   

                    </td>
                    <td><%=ngayLap %></td>
                    <td><%=soLuongDat %></td>
                    <td><%=gia %></td>
                    <td><%=tongtien %></td>
                    <td><%=datra %></td>
                    <td><%=email %></td>
                    <td><%=chuoiTinhtrang %></td>
              



                     <td>
                        <%if(tinhTrang == 0){ %>
                        <form action="/Manage/UpdateOrder" method="post">
                            <input type="submit" value="Accept"  class="btn" />
                            <input type="hidden" value="<%=maDonHang %>" name="maDH" />
                            <input type="hidden" value="2" name="tinhTrang" /> 
                        </form>
                        <%} %>
                    </td>
                    <td>
                        <%if(tinhTrang==0){ %>
                        <form action="/Manage/UpdateOrder" method="post">
                            <input type="submit" value="Reject" class="btn" />
                            <input type="hidden" value="<%=maDonHang %>" name="maDH" />
                            <input type="hidden" value="1" name="tinhTrang" />
                        </form>
                        <%} %>
                    </td>                    
                </tr>
                <tr>
                    <td colspan="9">
                         <div id="divChiTiet<%=maDonHang %>"></div>
                    </td>
                </tr>
            <%k++;
              }  %>
            <tr>
                <%
                    int tongtrangNhan = int.Parse(ViewData["tongTrangNhan"].ToString());
                    
                %>
                <td colspan="9">                    
                </td>
            </tr>
        </table>
        <div id="trigger-list">
                        <%for(int m = 0; m < tongtrangNhan; m++)  {
                        %>
                            <input type="button"class="trigger error-trigger" value="<%=m+1 %>" onclick ="loadOrdersRecived(<%=m%>)"/> 
                       
                        <%} %>
                    </div>
    </div>
</asp:Content>


  